/**
 * @所有关于数字的处理
 */
// 出去尾部0占位的保留小数
const clearZero = (str = '') => {
  const strArr = str.split('').reverse();
  const handleList: any[] = [];
  let findNoZero = false;
  strArr.forEach((item) => {
    if (findNoZero) handleList.push(item);
    else if (Number(item) !== 0) {
      findNoZero = true;
      handleList.push(item);
    }
  });
  return handleList.reverse().join('');
};
// 保留小数（末尾为0时要删除0）
export const removeTailFixed = (value: number, decimals: number) => {
  // 100.01 => 100.01; 100.00 => 100
  const times = Math.pow(10, decimals);
  const toFixedValue = (Math.floor(value * times) / times).toString();
  // const toFixedValue = value.toFixed(decimals);
  const arr = toFixedValue.split('.');
  const decimalsStr = clearZero(arr[1]);
  let str = arr[0];
  str += decimalsStr.length > 0 ? `.${decimalsStr}` : '';
  return str;
};

const styleFormat = (num: any, unit: string, returnHtml = true) => {
  if (returnHtml)
    return `<span style="font-family: number, sans-serif;font-weight: 600;">${num}</span>${unit}`;
  if (!returnHtml) return `${num}${unit}`;
};
export const numberInteger = (number: number, returnHtml = true) => {
  /**
   * 如果以最高位位补零加上加+：
   * 例如： 101 => 100+; 1001 => 1000+
   */
  if (number < 10) return styleFormat(number, '', returnHtml);
  if (number >= 10000) {
    const num = Math.floor(number / 10000);
    const numStr = num + '';
    let formatNum;
    if (numStr.length === 2) {
      formatNum = numStr.slice(0, 1) + '0';
      return styleFormat(formatNum, '万+', returnHtml);
    } else if (numStr.length > 2) {
      formatNum = numStr.slice(0, 2);
      for (let i = 0; i < numStr.length - 2; i++) {
        formatNum += '0';
      }
      return styleFormat(formatNum, '万+', returnHtml);
    } else {
      return styleFormat(numStr, '万+', returnHtml);
    }
  }
  const num = number + '';
  const value = num.slice(0, 1);
  const handleValue = Number(value.padEnd(num.length, '0'));
  return styleFormat(handleValue, '+', returnHtml);
};

export const numberFormat = (number: number, returnHtml = true) => {
  if (number >= 100000000) {
    const handleNumber = (number / 100000000).toFixed(0);
    if (returnHtml) return styleFormat(handleNumber, '亿', returnHtml);
    return `${handleNumber}亿`;
  } else if (number > 10000) {
    const handleNumber = (number / 10000).toFixed(0);
    if (returnHtml) return styleFormat(handleNumber, '万', returnHtml);
    return `${handleNumber}万`;
  } else {
    if (returnHtml) return styleFormat(number, '', returnHtml);
    return `${number}`;
  }
};
// 数字千分位逗号分割
export const micrometerNumber = (value: number) => {
  const number =
    value.toString().indexOf('.') !== -1
      ? value.toLocaleString()
      : value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  return number;
};
// 超过万亿时
export const yAisFormatterNumber = function (val: any, decimals = 1) {
  let srt = '';
  if (val >= 100000000) {
    srt = removeTailFixed(val / 100000000, decimals) + '亿';
  } else if (val >= 10000) {
    srt = removeTailFixed(val / 10000, decimals) + '万';
  } else {
    srt = removeTailFixed(val, decimals) + '';
  }
  return srt;
};
